home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / SYS / AMIGA / MALLOC.C < prev    next >
C/C++ Source or Header  |  1988-08-23  |  2KB  |  103 lines

  1. /*
  2.  * Hello Mic:
  3.  *
  4.  *   After mailing the first message to you about the free() function I
  5.  * decide on a solution similar to the one you sugested in your reply. There
  6.  * is one disadvantage with this solution it requires an extra 4 bytes for
  7.  * the link information beyond the original 8 bytes already required by each
  8.  * block.  If we can find a clean way of deallocating stuff within EMACS such
  9.  * as killing each buffer and all the other structures we will save 8 bytes
  10.  * per allocation (leaving only the 4 bytes for the block size). Anyways
  11.  * here is my doubly linked list version of free() which works very fast
  12.  * but takes up a bit more memory.
  13.  *
  14.  *      Later,
  15.  *         Leon
  16.  *
  17.  */
  18.  
  19. /* name: malloc.c
  20.  * desc: An improved malloc() and free() function for Aztec c
  21.  * date: 04/02/87 LF
  22.  * note: This takes over the Aztec _cln() function that is called by
  23.  *     the startup code once main() returns.
  24.  */
  25.  
  26. #define NULL 0L
  27.  
  28. struct mem {
  29.     struct mem *next, *prev;
  30.     long size;
  31. };
  32.  
  33. static struct mem *Free;
  34.  
  35. void *_AllocMem();
  36.  
  37. static
  38. cleanup()
  39. {
  40.     register struct mem *mp, *xp;
  41.  
  42.     for (mp=Free;mp;mp=xp) {
  43.         xp = mp->next;
  44.         _FreeMem(mp, mp->size+sizeof(struct mem));
  45.     }
  46.     Free = 0;
  47. }
  48.  
  49. char *
  50. lmalloc(size)
  51. unsigned long size;
  52. {
  53.     register struct mem *ptr;
  54.     extern int (*_cln)();
  55.  
  56.     _cln = cleanup;
  57.     if ((ptr = _AllocMem(size+sizeof(struct mem), 0L)) == 0)
  58.         return(0);
  59.     ptr->next = Free;
  60.         if (Free != NULL)
  61.         {
  62.           Free->prev = ptr;
  63.         }
  64.         ptr->prev = NULL;
  65.     ptr->size = size;
  66.     Free = ptr;
  67.     return((char *)ptr + sizeof(struct mem));
  68. }
  69.  
  70. char *
  71. malloc(size)
  72. unsigned size;
  73. {
  74.     return(lmalloc((unsigned long)size));
  75. }
  76.  
  77. free(blk)
  78. char *blk;
  79. {
  80.     register struct mem *mp, *xp, *xn;
  81.  
  82.         mp = (struct mem*)(blk - sizeof(struct mem));
  83.         xp = mp->prev;
  84.         xn = mp->next;
  85.  
  86.         if (xn != NULL)
  87.         {
  88.           xn->prev = xp;
  89.         }
  90.  
  91.         if (xp == NULL)
  92.         {
  93.           Free = xn;
  94.         }
  95.         else
  96.         {
  97.           xp->next = xn;
  98.         }
  99.  
  100.     _FreeMem(mp, mp->size+sizeof(struct mem));
  101.     return(0);
  102. }
  103.